﻿Imports System.Data.OleDb
Public Class AddEvent
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lbl_Error.Visible = False
    End Sub

    Protected Sub ddl_Categories_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Category.SelectedIndexChanged
        'Populate the second ddl (ddl_Beaches) when a user selects an activity displaying a list of which beaches have the selected activity.
        Dim ddlChoice As String = ddl_Category.SelectedValue
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM ActivityWithBeachName WHERE ActivityName ='" & ddlChoice & "' ORDER BY BeachName ASC"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlString, oleDbConn)
        Dim ds As New DataSet
        da.Fill(ds)
        ddl_Beaches.DataSource = ds
        ddl_Beaches.DataTextField = "BeachName" 'the items to be displayed in the list items
        ddl_Beaches.DataValueField = "Beaches_FK" 'the id of the items displayed"
        ddl_Beaches.DataBind()
    End Sub

    Protected Sub btn_AddEvent_Click(sender As Object, e As EventArgs) Handles btn_AddEvent.Click
        Dim ddlLocation As Integer = ddl_Beaches.SelectedValue
        'If user has not selected a location
        If ddlLocation < 1 Then
            lbl_Error.Visible = True
        Else
            'Get time from calendar extender
            tb_Date_CalendarExtender.SelectedDate = DateTime.ParseExact(tb_Date.Text, tb_Date_CalendarExtender.Format, Nothing)
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            Dim SqlString As String = "INSERT INTO Events(EventName, Beach_FK, DateOfEvent, EventCreator, Capacity, DateAdded) VALUES (@f1, @f2, @f3, @f4, @f5, Now())"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", ddl_Category.SelectedValue)
            cmd.Parameters.AddWithValue("@f2", ddl_Beaches.SelectedValue)
            cmd.Parameters.AddWithValue("@f3", tb_Date_CalendarExtender.SelectedDate)
            cmd.Parameters.AddWithValue("@f4", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f5", tb_Attendees.Text)
            oleDbConn.Open()
            cmd.ExecuteNonQuery()

            'If event creator is attending the event as well add him to the event attendees table
            If rbl_Attending.SelectedValue = -1 Then
                Dim eventID As Integer
                'Getting the event ID
                Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Events ORDER BY ID DESC", oleDbConn)
                Dim dr = getLastIdCmd.ExecuteReader()
                dr.Read()
                eventID = dr.GetValue(0)
                Dim AddSqlString As String = "INSERT INTO [Event_Attendees] ([Event_FK], [Attendee]) VALUES (@f1, @f2)"
                Dim cmdAdd As OleDbCommand = New OleDbCommand(AddSqlString, oleDbConn)
                cmdAdd.CommandType = CommandType.Text
                cmdAdd.Parameters.AddWithValue("@f1", eventID)
                cmdAdd.Parameters.AddWithValue("@f2", User.Identity.Name)
                cmdAdd.ExecuteNonQuery()
            End If

            oleDbConn.Close()

            Response.Redirect("Events.aspx")
        End If
    End Sub

End Class